package cn.wynu.ioc.annotation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

// @Component 相当于之前写的<bean class="cn.wynu.ioc.annotation.Student1"> </bean>
@Component
// 相当于 scope 属性，支持两种模式：singleton和prototype
//@Scope(value = "prototype")
public class Student1 {

    @Value("张三")
    String name;

    // 相当于在xml配置文件里面的 <property name="learnId" value="0205"/>
    @Value("0205")
    String learnId;

    // 和@Value功能类似，给属性赋值，不过@Autowired针对引用数据类型
    @Autowired
    Home home;

    @Override
    public String toString() {
        return "Student1{" +
                "name='" + name + '\'' +
                ", learnId='" + learnId + '\'' +
                ", home=" + home +
                '}';
    }

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(AnnotationConfig.class);

        Student1 bean = context.getBean(Student1.class);
        Student1 bean1 = context.getBean(Student1.class);

        System.out.println(bean);
        System.out.println(bean1);
    }
}
